// -*- C++ -*-

//=============================================================================
/**
*  @file    Service_Type.h
*
*	@date    2009-12-17 16:42
*
*  @author wing <totti19851101@gmail.com>
*/
//=============================================================================

#ifndef _SERVICE_TYPE_H_
#define _SERVICE_TYPE_H_

#include "Pre.h"
#include "DLL.h"

#if defined (WIN32)
	typedef HINSTANCE SHLIB_HANDLE;
#else
	typedef void* SHLIB_HANDLE;
#endif

// Forward decl.
class Service_Type_Impl;

/**
* @class Service_Type
*
* @brief Keeps track of information related to the various
* Service_Type_Impl subclasses.
*
* This class acts as the interface of the "Bridge" pattern.
*/

class Service_Type
{
public:
	enum
	{
		/// Delete the payload object.
		DELETE_OBJ = 1,
		/// Delete the enclosing object.
		DELETE_THIS = 2
	};

	enum
	{
		SERVICE_OBJECT = ACE_SVC_OBJ_T,
		MODULE = ACE_MODULE_T,
		STREAM = ACE_STREAM_T,
		INVALID_TYPE = -1
	};

	// = Initialization and termination methods.
	Service_Type( const char* n,
				Service_Type_Impl* o,
				const DLL& dll,
				int active );
	Service_Type( const char* n,
				Service_Type_Impl* o,
				SHLIB_HANDLE handle,
				int active );
	~Service_Type( void );

	const char* name( void ) const;
	void name( const char* );

	const Service_Type_Impl* type( void ) const;
	void type( const Service_Type_Impl*, int active = 1 );

	int suspend( void ) const;
	int resume( void ) const;
	int active( void ) const;
	void active( int );

	/// calls <fini> on <type_>
	int fini( void );

	/// check if the service has been fini'ed.
	int fini_called( void ) const;

	/// Dump the state of an object.
	void dump( void ) const;

private:
	/// Humanly readible name of svc.
	const char* name_;

	/// Pointer to c++ object that implements the svc.
	const Service_Type_Impl* type_;

	/// DLL representint the shared object file(non-zero if dynamically linked)
	mutable DLL dll_;

	/// 1 if svc is currently active, otherwise 0.
	int active_;

	/// 1 if <fini> on <type_> has already been called, otherwise 0.
	int fini_already_called_;
};

#include "Post.h"
#endif//_SERVICE_TYPE_H_